﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using CarbonExchange.Bll;

public partial class PledgeCategories : Acms.AcmsControls.ContentControls.Admin.BaseCustomAdminControl
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dsPledgeCategories.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(dsPledgeCategories_Select);
        dsPledgeCategories.Insert += new EventHandler<Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs>(dsPledgeCategories_Save);
        dsPledgeCategories.Update += new EventHandler<Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs>(dsPledgeCategories_Save);
        dsPledgeCategories.Delete += new EventHandler<Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs>(dsPledgeCategories_Delete);
        dsSections.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(dsSections_Select);
        sections.ItemCreated += new EventHandler<ListViewItemEventArgs>(sections_ItemCreated);
    }

    void sections_ItemCreated(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            var tmp = e.Item as ListViewDataItem;
            if (tmp.DisplayIndex == sections.SelectedIndex)
            {
                var grid = e.Item.FindControl("grid") as RadGrid;
                if (grid != null)
                {
                    var col = grid.Columns.OfType<GridHyperLinkColumn>().Where(x => x.UniqueName == "Pledges").SingleOrDefault();
                    if (col != null)
                    {
                        col.DataNavigateUrlFormatString = base.ResolveCustomControlPath("~/Pledges.ascx?Id={0}");
                    }
                }
            }
        }
    }

    void dsPledgeCategories_Delete(object sender, Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs e)
    {
        var factory = new CarbonExchange.Bll.PledgeCategoryFactory();
        if (e.Operation == Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs.Operations.Delete)
        {
            var id = (int)e.Keys["Id"];
            var cid = e.Keys["CID"];
            var tmp = factory.GetById(id);
            tmp.EnsureConcurent(cid);
            factory.Delete(tmp);
        }
    }

    void dsPledgeCategories_Save(object sender, Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs e)
    {
        var factory = new CarbonExchange.Bll.PledgeCategoryFactory();
        CarbonExchange.Bll.PledgeCategory tmp = null;
        if (e.Operation == Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs.Operations.Insert)
        {
            tmp = new CarbonExchange.Bll.PledgeCategory();
            tmp.SectionId = (int)sections.SelectedDataKey["Id"]; ;
        }
        else
        {
            var id = (int)e.Keys["Id"];
            var cid = e.Keys["CID"];
            tmp = factory.GetById(id);
            tmp.EnsureConcurent(cid);
        }

        tmp.Info = e.NewValues["Info"] as string;
        tmp.Name = e.NewValues["Name"] as string;

        factory.Save(tmp);
    }

    void dsSections_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        var factory = new CarbonExchange.Bll.SectionFactory();
        e.SetData(factory.GetAll());
    }

    void dsPledgeCategories_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        e.SetData(GetPledgeCategories());
    }

    private IList<PledgeCategory> GetPledgeCategories()
    {
        int id = (int)sections.SelectedDataKey["Id"];
        var factory = new CarbonExchange.Bll.PledgeCategoryFactory();
        return factory.GetAllBySectionId(id).ToList();
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void grid_ItemCommand(object sender, GridCommandEventArgs e)
    {
        var grid = sender as RadGrid;
        if(grid != null)
        {
            if (e.CommandName == "MoveUp" || e.CommandName == "MoveDown")
            {
                var factory = new CarbonExchange.Bll.PledgeCategoryFactory();

                int step = e.CommandName == "MoveUp" ? -1 : 1;
                int index = e.Item.DataSetIndex;
                var items = GetPledgeCategories();
                factory.Move(items, index, index + step);
                grid.SelectedIndexes.Clear();
                grid.Rebind();
                grid.MasterTableView.Items[e.Item.ItemIndex + step].Selected = true;
            }
        }
    }

}
